package com.sks.web.action.deployment;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;
import com.sks.bean.PageView;
import com.sks.bean.pojo.Favorites;
import com.sks.bean.pojo.House;
import com.sks.bean.pojo.Users;
import com.sks.service.house.FavoritesService;
import com.sks.service.house.HouseService;
import com.sks.utils.WebUtil;
import com.sks.web.action.privilege.Permission;
/**
 * 外部用户的房源列表
 */
@Controller("/sks/frontpages/privilege/deployment/list")
public class DeploymentListAction extends Action {
	@Resource HouseService service;

	@Resource(name = "favoritesServiceBean") private FavoritesService favoritiesService;
	@Override @Permission(module="users", privilege="view")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		PageView<House> pageView = new PageView<House>(12, 1);		//分页容器
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		Users users = WebUtil.getUsers(request);	//获取注册用户
		if(users != null){
			orderby.put("houseId", "desc");		//排序
			StringBuilder jpql = new StringBuilder();		//
			List<Object> params = new ArrayList<Object>();
			params.add( users );
			jpql.append("o.users = ?").append(params.size());
			//执行查询，并添加到分页中
			pageView.setQueryResult(
					service.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(),
								jpql.toString(), params.toArray(), orderby));
			request.setAttribute("pageView", pageView);
			common(request);
			return mapping.findForward("list");
		}
		return mapping.findForward("error");
	}
	
private void common (HttpServletRequest request){
		
		StringBuffer build = new StringBuffer(" o.user.userId = '" + WebUtil.getUsers(request).getUserId() + "'");
		build.append(" and o.building.buildingId is not null");
		PageView<Favorites> pv = new PageView<Favorites>(9999999, 0);//9999999是显示的最大结果
	    pv.setQueryResult(favoritiesService.getScrollData(0, pv.getMaxresult(), build.toString(),null , null));	    
	    request.setAttribute("buildNum", pv.getTotalrecord());  
	    request.setAttribute("pv", pv);  
	    
	    
	    //获取房源
	    StringBuffer house = new StringBuffer(" o.user.userId = '" + WebUtil.getUsers(request).getUserId() + "'");
	    house.append(" and o.house.houseId is not null");
		PageView<Favorites> page = new PageView<Favorites>(9999999, 0);//9999999是显示的最大结果
		page.setQueryResult(favoritiesService.getScrollData(0, page.getMaxresult(), house.toString(),null , null)); 
		request.setAttribute("houseNum", page.getTotalrecord());
		request.setAttribute("page", page);
	}
}
